package com.leadership.entity.user;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import com.demo2do.core.format.PersistentFormat;
import com.leadership.entity.idm.Organization;

/**
 *
 * @author Ark
 *
 */
@Entity
@Table(name = "principal")
public class Principal {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /** 用户名  */
    private String username;
    
    /** 密码  */
    private String password;
    
    private String originPassword;
    
    /** 权限  */
    private String authority;
    
    /** 手机号  */
    private String email;
    
    /** 姓名 */
    private String name;
    
    /** 部门 ID */
    @ManyToOne(fetch = FetchType.EAGER, optional = true)
    @PersistentFormat
    private Organization organization;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createDate;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date updateDate;
    
    /** 是否启用 */
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public Principal() {
    }
    
    /**
     * 
     * @param password
     * @return
     */
    public String encodePassword(String password) {
        this.username = this.email.split("@")[0];
        String saltPassword = StringUtils.join(password, "{", this.email, "}");
        return DigestUtils.md5Hex(saltPassword);
    }
    
    /**
     * 
     * @param password
     * @return
     */
    public Principal changePassword(String password) {
        this.username = this.email.split("@")[0];
        String saltPassword = StringUtils.join(password, "{", this.email, "}");
        this.password = DigestUtils.md5Hex(saltPassword);
        return this;
    }
    
    /**
     * @param date
     * @return
     */
    public Principal init(Date date) {
        this.username = this.email.split("@")[0];
        this.createDate = date;
        this.updateDate = date;
        return this;
    }
    
    /**
     * @param organization
     * @param date
     * @return
     */
    public Principal updateOrganization(Organization organization, Date date) {
        this.username = this.email.split("@")[0];
        this.organization = organization;
        this.createDate = date;
        this.updateDate = date;
        return this;
    }
    
    /**
     * @param organization
     * @param date
     * @return
     */
    public Principal evaluateOrganization(Organization organization, Date date) {
        this.username = this.email.split("@")[0];
        this.organization = organization;
        this.updateDate =date;
        return this;
    }
    
    public Principal update(Date date){
        this.username = this.email.split("@")[0];
        this.updateDate=date;
        return this;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @return the originPassword
     */
    public String getOriginPassword() {
        return originPassword;
    }

    /**
     * @return the authority
     */
    public String getAuthority() {
        return authority;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the organization
     */
    public Organization getOrganization() {
        return organization;
    }

    /**
     * @return the createDate
     */
    public Date getCreateDate() {
        return createDate;
    }

    /**
     * @return the updateDate
     */
    public Date getUpdateDate() {
        return updateDate;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @param originPassword the originPassword to set
     */
    public void setOriginPassword(String originPassword) {
        this.originPassword = originPassword;
    }

    /**
     * @param authority the authority to set
     */
    public void setAuthority(String authority) {
        this.authority = authority;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param organization the organization to set
     */
    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    /**
     * @param createDate the createDate to set
     */
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    /**
     * @param updateDate the updateDate to set
     */
    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
}
